﻿var $FileUploaderSingle = function (opts, event) {
	var _enum = {
		Q_EXCEED_NUM_LIMIT: "添加的文件数量已超出最大限制数量",
		Q_EXCEED_SIZE_LIMIT: "添加的文件过大了",
		Q_TYPE_DENIED: "不支持该类型文件加入上传列表",
		F_EXCEED_SIZE: "添加的文件过大了"
	}

	var _defaultOpts = {
	    dom: {
	        uploadBtn: "#btn-upload",
	        filePrefix: "",
	    }, upload: {
	        server: "http://api.manage.meiyungou.jshcn.cn/upload/pic",
	        accept: {
	            title: 'Images',
	            extensions: 'gif,jpg,jpeg,bmp,png',
	            mimeTypes: 'image/*'
	        },
	        pick: {
	            multiple: true, id: "#btn-upload", innerHTML: "选择文件"
	        },
	        auto: true,
	        thumb: {
	            width: 110,
	            height: 110,
	            quality: 100,
	            allowMagnify: true,
	            crop: true
	        },
	        threads: 1,
	        fileVal: "fileupload",
	        fileNumLimit: 1,
	        fileSingleSizeLimit: 1 * 1024 * 1024,
	    },
	    event: {
	        succee: function () { }
	    }
	};
	opts = Object.assign(_defaultOpts, opts);
	opts.upload.pick.id = opts.dom.uploadBtn;

	var _WebUploader = WebUploader.create(opts);

	//当文件被加入队列之前触发
	_WebUploader.on("beforeFileQueued", function (file) {
		$uploader.on('error', function (handler) {
			var msg = eval("$_enum." + handler);
			opts.event.beforeFileQueuedError(msg);
		})
	})

	//当文件被加入队列以后触发
	_WebUploader.on("fileQueued", function (file) {
		_WebUploader.makeThumb(file, function (error, ret) {
		    opts.event.queued(file, error, ret);
		})
	})

	//当文件被移除队列后触发 删除实时显示文件效果图
	_WebUploader.on("fileDequeued", function (file) {
	    opts.event.dequeued(file);
	});

	//上传进度条
	_WebUploader.on("uploadProgress", function (file, Number) {
	    opts.event.progress(file, Number);
	})

	//上传成功触发
	_WebUploader.on("uploadSuccess", function (file, response) {
	    var data = JSON.parse(response._raw);
	    if (opts.dom.filePrefix != "") {
	        data.url = opts.dom.filePrefix + data.url;
	    }
	    opts.event.succee(data);
	});

	//上传错误 
	_WebUploader.on("uploadError", function (file, response) {
	    opts.event.error(file, response);
	});

	//不管成功失败都会调用
	_WebUploader.on("uploadComplete", function (file) {
	    opts.event.complet(file);
	})

	opts.event.removeFile(_WebUploader);

}